<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">卡券核销<image style="width:82rpx;height:42rpx;" src="/static/images/arrow-r.png"></image>完成服务</view>
			<view class="input-box">
				<input class="u-border-bottom" type="text" v-model="voucher_code" placeholder="请输入核销卡券" />
				<view class="right-btn clear" v-if="voucher_code" @click="clearInput">清空</view>
				<view class="right-btn clear" v-if="!voucher_code" @click="scanQrCode">扫一扫</view>
			</view>
			<br>
			<button @tap="queryVoucher" :style="[inputVoucherCode]" class="getCaptcha">查询卡券</button>
		</view>

	</view>
</template>

<script>
	var wx = require('jweixin-module')
	
	export default {
		data() {
			return {
				title: '',
				buttonText: '',
				action: '',
				payment_id: '',
				voucher_code: '',
				wxConfig:{}
			}
		},
		computed: {
			inputPaymentId() {
				let style = {};
				if (this.payment_id) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			},
			inputVoucherCode() {
				let style = {};
				if (this.voucher_code) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		onLoad(options) {
			this.action = options.action;
		},
		methods: {
			getWxConfig() {
				let that = this;
			
				this.$u.post('/shopping/wx-js-sdk-config', {
					current_page: location.href.split('#')[0]
				}).then(res => {
					if(!res.current_uid){
						return;
					}
					console.log(res)
					this.wxConfig = res
					wx.config(this.wxConfig)
					//jweixin.config(res);
					// jweixin.ready(function() {
					// 	// TODO
					// 	console.log('wx-is ready')
					// });
					// jweixin.error(function(res) {
					// 	console.log(res)
					// 	// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					// });
				}).catch(e => {
				})
			},
			queryVoucher(){
				if(!this.voucher_code){
					uni.showToast({
						title: '请输入核销码',
						icon: 'none'
					});
					return false;
				}
				this.$u.route({
					type: 'to',
					params: {
						voucher_code: this.voucher_code
					},
					url: '/pages/ucp/store-manage/voucher-ucp-confirm',
					animationType: 'slide-in-bottom'
				});
				return;
			},
			clearInput(){
				this.voucher_code = ''
			},
			scanQrCode(){
				let that = this
				wx.scanQRCode({
				  needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
				  scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
				  success: function (res) {
				    that.voucher_code = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
				  }
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;
		background-color:#fff;
		padding: 40rpx;
		margin: 20rpx;
		border-radius: 4px;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;
			
			.input-box{
				display: flex;
				background-color: #f6f6f6;
				padding: 40rpx;
				input{
					flex: 1;
					text-transform: uppercase;
				}
				.right-btn{
					display: inline-block;
					width: 4em;
					text-align: right;
				}
			}

			.title {
				text-align: left;
				font-size: 50rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
				text-align: center;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 350rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}

		.payment-info {
			background-color: #FFF;
			padding: 0 30rpx 30rpx 30rpx;
			border-radius: 10rpx;
			box-sizing: border-box;
			margin-bottom: 30rpx;

			.bold {
				font-weight: bold;
				border-bottom: 1rpx solid #c9c8d2;
			}

			.calculation-data {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				padding-top: 30rpx;
				// margin-bottom: 10rpx;

				.data-title {
					width: 6em;
					font-weight: bold;
				}

				.data-text {
					flex: 1
				}
			}

		}
	}
</style>
